<?php

namespace app\admin\controller;

use app\admin\model\Column;

class ColumnController extends BaseController
{
    public function index()
    {
        return $this->fetch();
    }
    public function read()
    {
        $Column = new Column();
        $data =$Column->order('sort asc');
        $data = $data->select();
        if (isset($_GET['data_type'])&& !empty($_GET['data_type'])){
            $alias = ['column_name' => 'name', 'column_parent' => 'pid', 'column_id' => 'id'];
            $tree_list = tree($data, 'column_id', $alias);
            array_unshift($tree_list, ['name' => '顶级栏目', 'pid' => 0, 'id' => 0]);
            return json($tree_list);
        }
        return json(array('code' => 0, 'msg' => "", 'data' => $data, 'count' => 100));
    }

    public function del()
    {
        $data = input('post.');
        $result = Column::get(input('post.'))->allowfield(true)->delete();
        if ($result) {
            return json(['code' => 0, 'msg' => '删除成功']);
        } else {
            return json(['code' => 500, 'msg' => '删除失败']);
        }
    }
    public function add()
    {
        $column= (new Column());

        $column->save(input('post.'));
        if ($column) {
            return json(['code' => 0, 'msg' => '添加成功']);
        } else {
            return json(['code' => 500, 'msg' => '添加失败']);
        }
    }
    public function upd()
    {
        $column= (new Column());
        $post_data = input('post.');
        $column->isUpdate(true)->save($post_data);
        if ($column) {
            return json(['code' => 0, 'msg' => '更新成功']);
        } else {
            return json(['code' => 500, 'msg' => '更新失败']);
        }
    }

}
